package org.reservation.dao.impl;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import org.reservation.dao.FoodDAO;
import org.reservation.pojo.Food;
import org.reservation.pojo.Restaurant;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

public class FoodDAOHiberImpl extends HibernateDaoSupport implements FoodDAO{

	public void addFood(Food food) {
		// TODO Auto-generated method stub
		getHibernateTemplate().save(food);
	}

	public void deleteFoodById(int foodId) {
		// TODO Auto-generated method stub
		Food food=getHibernateTemplate().get(Food.class, foodId);
		getHibernateTemplate().delete(food);
	}

	public Food getFoodById(int foodId) {
		// TODO Auto-generated method stub
		Food food=getHibernateTemplate().get(Food.class, foodId);
		return food;
	}

	public List<Food> getFoodByRes(int resId) {
		// TODO Auto-generated method stub
		final Restaurant res=getHibernateTemplate().get(Restaurant.class,resId);
		//List<Food> foodList=getHibernateTemplate().find("from Food where restaurant=?",res);
		List list = getHibernateTemplate().executeFind(new HibernateCallback() {
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				Criteria cri = session.createCriteria(Food.class);
				cri.add(Restrictions.eq("restaurant", res));
				List<Restaurant> result = cri.list();
		return result;
	}
		});
		return list;
	}
	public List<Food> loadFood() {
		// TODO Auto-generated method stub
		List<Food> foodList=getHibernateTemplate().find("from Food");
		return foodList;
	}

	public void updateFood(Food food) {
		// TODO Auto-generated method stub
		getHibernateTemplate().update(food);
	}



}
